package com.shouhuyun.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.shouhuyun.constant.MessageConstant;
import com.shouhuyun.entity.PageResult;
import com.shouhuyun.entity.QueryPageBean;
import com.shouhuyun.entity.Result;
import com.shouhuyun.pojo.CheckItem;
import com.shouhuyun.pojo.Permission;
import com.shouhuyun.service.PermissionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;



    // 新增角色的时候 查询权限列表
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = permissionService.pageQuery(queryPageBean);
        return pageResult;
    }

    /**
     * 权限的增加
     * @param permission
     * @return
     */
    @RequestMapping("/add")
    public Result addPermission(@RequestBody Permission permission){

        try {
            permissionService.add(permission);
            return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_PERMISSION_FAIL);
        }
    }

    /**
     *  权限的删除
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result deletePermission(Integer id){
        try {
            // 删除以后返回一个值
            Integer num = permissionService.delete(id);
            if (num == 0){
                // 删除成功
                return new Result(true,MessageConstant.DELETE_PERMISSION_SUCCESS);
            }else {
                // 后台正确 但是权限有关联导致删除失败
                return new Result(false,MessageConstant.DELETE_PERMISSION_ROLE_FAIL);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_PERMISSION_FAIL);

        }
    }

    //b编辑权限管理
    @RequestMapping("/edit")
    public Result edit(@RequestBody Permission permission){
        try{
            permissionService.edit(permission);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, "编辑权限管理失败");
        }
        return  new Result(true,"编辑权限管理成功");
    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
          Permission permission= permissionService.findById(id);
            return  new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,permission);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
}
